package Messages;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;
import server.SQLConnection;
/**
 * This class implements the Add raw materials  to DB
 * @author Gabi Revah
 * @author Aviad Almog
 */
public class MessageAddRaw {
	private ConnectionToClient client ;
	private String[] str; 

/**
 * Constructor of this class 
 * @param str
 * @param client
 */
	public MessageAddRaw(String[] str, ConnectionToClient client) {
		this.client= client ;
		this.str= str; 	
	}
	
/**
 * Adding Raw Material to the DB
 * @throws SQLException
 * @throws IOException
 */
	
	public void AddRaw() throws SQLException, IOException{
		SQLConnection sc = new SQLConnection();
		java.sql.PreparedStatement raw = sc.getConnection().prepareStatement("SELECT * FROM rawmaterial where name='"+str[1]+"' and suppID='"+str[2]+"';");
		ResultSet rwrs= raw.executeQuery();
		if(rwrs.next()){
			ArrayList<Object> bus=new ArrayList<Object>();
			bus.add("RawExist");		
			client.sendToClient(bus);
		}
		else{
			java.sql.PreparedStatement vsins = sc.getConnection().prepareStatement("INSERT INTO rawmaterial (name,suppID,units,inventorystock,rentstock) VALUES(?,?,?,?,?);");		
				vsins.setString(1,str[1]);					
				vsins.setString(2,str[2]);
				vsins.setString(3,str[3]);
				vsins.setString(4,str[4]);
				vsins.setString(5,"0");
				vsins.executeUpdate();
				vsins.close();
				ArrayList<Object> bus=new ArrayList<Object>();
				bus.add("AddRaw");		
				client.sendToClient(bus);
			}
		
	}
}
